package com.wxxymaker.edu.service;

import com.blade.ioc.annotation.Bean;
import com.wxxymaker.edu.model.entity.Ask;
import com.wxxymaker.edu.model.entity.AskResult;
import com.wxxymaker.edu.model.entity.User;
import io.github.biezhi.anima.Anima;
import io.github.biezhi.anima.core.AnimaQuery;
import io.github.biezhi.anima.core.Joins;
import io.github.biezhi.anima.enums.OrderBy;
import io.github.biezhi.anima.page.Page;
import lombok.extern.slf4j.Slf4j;

import static io.github.biezhi.anima.Anima.select;

/**
 * @author zyx
 * @date 2018/10/14 - 11:02
 */
@Bean
@Slf4j
public class AskResultService implements Service<AskResult> {

    @Override
    public AskResult add(AskResult askResult) {
        boolean rollback = Anima.atomic(() -> {
            Integer pk = askResult.save().asInt();
            askResult.setId(pk);
        }).catchException(e -> {
            log.error("添加失败{}", e.getMessage());
        }).isRollback();
        return rollback ? null : askResult;
    }

    @Override
    public AskResult delete(Object pk) {
        AskResult askResult = byId(pk);
        boolean rollback = Anima.atomic(askResult::delete)
                .catchException(e -> {
                    log.error("删除失败!{}", e.getMessage());
                }).isRollback();
        return rollback ? null : askResult;
    }

    @Override
    public boolean change(AskResult askResult) {
        return false;
    }

    @Override
    public AskResult byId(Object pk) {
        return (select().from(AskResult.class).byId(pk));
    }

    public AskResult getAskResultById(Integer id) {
        AnimaQuery<AskResult> animaQuery = Anima.select().from(AskResult.class)
                .join(Joins.with(User.class)
                        .as(AskResult::getFrom)
                        .on(AskResult::getFromUserId, User::getId))
                .join(Joins.with(User.class)
                        .as(AskResult::getTo)
                        .on(AskResult::getToUserId, User::getId))
                .join(Joins.with(Ask.class)
                        .as(AskResult::getAsk)
                        .on(AskResult::getAskId, Ask::getId));
        if (id > 0) {
            animaQuery.where(AskResult::getId, id);
        }
        return animaQuery.page(1,1).getRows().get(0);
    }

    public Page<AskResult> getAskResultByWid(Integer wid, Integer page, Integer size) {
        AnimaQuery<AskResult> askResultAnimaQuery = Anima.select()
                .from(AskResult.class)
                .join(Joins.with(User.class)
                        .as(AskResult::getFrom)
                        .on(AskResult::getFromUserId, User::getId))
                .join(Joins.with(User.class)
                        .as(AskResult::getTo)
                        .on(AskResult::getToUserId, User::getId))
                .join(Joins.with(Ask.class)
                        .as(AskResult::getAsk)
                        .on(AskResult::getAskId, Ask::getId))
                .order(AskResult::getTime, OrderBy.DESC);

        if (wid > 0) {
            askResultAnimaQuery.where(AskResult::getAskId, wid);
        }
        return askResultAnimaQuery.page(page, size);
    }

    public Long countByUid(Integer userId) {
        return Anima.select().from(AskResult.class)
                .where(AskResult::getFromUserId, userId)
                .count();
    }

    public int deleteByAid(Integer askId) {
        return Anima.delete().from(AskResult.class)
                .where(AskResult::getAskId, askId)
                .execute();
    }
}
